<template>

  <div class="cage">
  <div><el-button type='primary' @click="addshow">添加分类</el-button></div>
  <p>分类列表</p>
  <el-card>
  
  <el-table
    :data="tableData"
    border
    style="width: 100%">
    <el-table-column
      prop="cate_name"
      label="序号"
     >
    </el-table-column>
    <el-table-column
      prop="cate_alias"
      label="分类名称"
     >
    </el-table-column>
    <el-table-column
     
      label="操作">
<template slot-scope="scope">
<el-button type='primary'>编辑</el-button>
<el-button type='danger'>删除</el-button>
 
</template>

    </el-table-column>
  </el-table>
  </el-card>
  <!-- 对话框 -->
  <el-dialog
  title="提示"
  :visible.sync="dialogVisible"
   width="50%"

  label-width="100px"
  >
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
   <el-form-item  prop="cate_name" label='分类名称'>
    <el-input v-model="ruleForm.cate_name" ></el-input>
  </el-form-item>
  <el-form-item prop="cate_alias" label='分类类别'>
    <el-input v-model="ruleForm.cate_alias" ></el-input>
  </el-form-item> 
   </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="config">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import {updatacatelist,updatacateadd} from '@/api/http'
  export default {
    data() {
      return {
        tableData: [],
        dialogVisible:false,
         ruleForm:{
          cate_name:'',
          cate_alias:'',

        },
         rules: {
          
           cate_name: [
            { required: true, message: '请输入分类名称', trigger: 'blur' },
          
        
         
          ],
          cate_alias: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
         },
      }
    },
    methods: {
   async   updatacatelist(){
        const {data:res} = await updatacatelist()
        console.log(res)
        this.tableData=res.data

        if(res.code!==0){
        this.$message.error('分类列表获取失败');
        }
        
      },
      addshow(){
        this.dialogVisible=true
      },
      config(){
        this.$refs.ruleForm.validate(async (valid) => {
          if (valid) {
            const {data:res} = await updatacateadd(this.ruleForm)
            console.log(res)
             if(res.code===0){
        this.$message.success('添加成功');
         this.updatacatelist()
             this.dialogVisible=false
        }else{
             this.$message.error('添加分类失败');
        }
           
          }
      })
      },
    },
    created() {
      this.updatacatelist()
    },
  }
</script>
<style lang="less" scoped>
  .cage{
    line-height: 10px;
  }
  .el-input{
    width: 300px;
  }
</style>